library(glatos)
library(sf)
library(mapview)
library(plotly)
library(tidyverse)
det_path <- file.path("..", "data", "detections.csv")

detection_events <- 
  read_glatos_detections(det_path) %>% 
  false_detections(tf = 3600) %>% 
  filter(passed_filter != FALSE) %>% 
  detection_events(location_col = 'station')
## The filter identified 93 (1.3%) of 7180 detections as potentially false.
## The event filter distilled 7087 detections down to 3537 distinct detection events.
rx_file <- file.path("..", "data", "deployments.csv")

receivers <-
  read_glatos_receivers(rx_file)
## Combine detection and reciever datasets into a single combined data frame
combined_data <- 
  detection_events %>% 
  left_join(receivers, by = c("location" = "station")) %>% 
  filter(first_detection >= deploy_date_time, first_detection <= recover_date_time)

head(combined_data)
##   event animal_id location mean_latitude mean_longitude
## 1     1       153  TTB-002      43.39165      -83.99264
## 2     2       153  TTB-001      43.38709      -83.98737
## 3     3       153  TTB-002      43.39165      -83.99264
## 4     4       153  TTB-001      43.38709      -83.98737
## 5     5       153  TTB-002      43.39165      -83.99264
## 6     6       153  TTB-001      43.38709      -83.98737
##       first_detection      last_detection num_detections res_time_sec
## 1 2012-04-29 01:48:37 2012-04-29 02:05:33              8         1016
## 2 2012-04-29 02:08:00 2012-04-29 02:08:00              1            0
## 3 2012-04-29 02:08:00 2012-04-29 02:08:00              1            0
## 4 2012-04-29 02:09:50 2012-04-29 02:09:50              1            0
## 5 2012-04-29 02:09:50 2012-04-29 02:09:50              1            0
## 6 2012-04-29 02:12:24 2012-04-29 02:12:24              1            0
##   glatos_array station_no consecutive_deploy_no intend_lat intend_long
## 1          TTB          2                     3         NA          NA
## 2          TTB          1                     3         NA          NA
## 3          TTB          2                     3         NA          NA
## 4          TTB          1                     3         NA          NA
## 5          TTB          2                     3         NA          NA
## 6          TTB          1                     3         NA          NA
##   deploy_lat deploy_long recover_lat recover_long    deploy_date_time
## 1   43.39165   -83.99264          NA           NA 2012-03-14 16:50:00
## 2   43.38709   -83.98737          NA           NA 2012-03-14 16:35:00
## 3   43.39165   -83.99264          NA           NA 2012-03-14 16:50:00
## 4   43.38709   -83.98737          NA           NA 2012-03-14 16:35:00
## 5   43.39165   -83.99264          NA           NA 2012-03-14 16:50:00
## 6   43.38709   -83.98737          NA           NA 2012-03-14 16:35:00
##     recover_date_time bottom_depth riser_length instrument_depth
## 1 2012-07-17 17:04:00           NA           NA               NA
## 2 2012-07-17 16:20:00           NA           NA               NA
## 3 2012-07-17 17:04:00           NA           NA               NA
## 4 2012-07-17 16:20:00           NA           NA               NA
## 5 2012-07-17 17:04:00           NA           NA               NA
## 6 2012-07-17 16:20:00           NA           NA               NA
##   ins_model_no glatos_ins_frequency ins_serial_no deployed_by comments
## 1         VR2W                   69        113213                     
## 2         VR2W                   69        119331                     
## 3         VR2W                   69        113213                     
## 4         VR2W                   69        119331                     
## 5         VR2W                   69        113213                     
## 6         VR2W                   69        119331                     
##   glatos_seasonal glatos_project glatos_vps
## 1             YES          HECWL         NO
## 2             YES          HECWL         NO
## 3             YES          HECWL         NO
## 4             YES          HECWL         NO
## 5             YES          HECWL         NO
## 6             YES          HECWL         NO
## Plot your combined dataset 
combined_sf_data <- combined_data %>% 
  group_by(animal_id, location, deploy_lat, deploy_long) %>% 
  summarise(Num.Det = n()) %>% 
  st_as_sf(coords = c("deploy_long", "deploy_lat"), crs = 4326) %>% 
  mapview(zcol = "animal_id", cex = "Num.Det", burst = T, legend = F)

combined_sf_data
library(gganimate)
library(ggmap)
library(lubridate)
plot_data <-
  combined_data %>% 
  mutate(timestep = round_date(first_detection, unit = "1 days")) %>% 
  group_by(timestep, animal_id) %>% 
  summarise(lon = mean(deploy_long),
            lat = mean(deploy_lat))
base <- 
  get_stamenmap(
    bbox = c(left = min(plot_data$lon),
             bottom = min(plot_data$lat), 
             right = max(plot_data$lon), 
             top = max(plot_data$lat)),
    maptype = "toner-lite",
    crop = F, 
    zoom = 8)
walleye.plot <-
  ggmap(base) +
  geom_point(data = plot_data, aes(x = lon, y = lat, group = animal_id, color = animal_id), size = 2) +
  geom_path(data = plot_data, aes(x = lon, y = lat, group = animal_id, color = animal_id)) +
  labs(title = "Walleye animation",
       x = "Longitude", y = "Latitude", color = "Tag ID")

walleye.plot

ggplotly(walleye.plot)
walleye.animation <-
  walleye.plot +
  labs(subtitle = 'Date: {format(frame_along, "%d %b %Y")}') +
  transition_reveal(timestep) +
  shadow_mark(past = T, future = F)